package srdb.migration.script;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

public class SplitScriptFile {
	public static void main(String[] args) throws IOException{
	  String fileNum = ScriptUtil.getArg(args, "num");
	  int num=Integer.parseInt(fileNum);
	  String fileNameTemplate = ScriptUtil.getArg(args, "file_name_template");
	  String file = ScriptUtil.getArg(args, "file");
	  
	  File splitFile=new File(file);
	  List<String> lines=FileUtils.readLines(splitFile);
	  
	  int pageNum=lines.size()/num;
	  
	  for(int i=0;i<num;i++){
		  List<String> tmpLines=new ArrayList<String>();
		  
		  for(int j=0;j<=pageNum;j++){
			  int index=i*num+j;
			  if(index<lines.size()){
				  tmpLines.add(lines.get(index)); 
			  }
			  
			  
		  }
		  String fileName=StringUtils.replace(fileNameTemplate, "____", String.valueOf(i+1));
		  File newFile=new File(fileName);
		  FileUtils.writeLines(newFile, tmpLines);
		  
		  
	  }
	  
	  
		
	}
}
